package Tests;

import org.junit.jupiter.api.*;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvFileSource;
import org.junit.jupiter.params.provider.CsvSource;
import org.openqa.selenium.By;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.util.concurrent.TimeUnit;

import static java.lang.Thread.sleep;

@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class ForumLoginTest extends AutoTestUtils {
    @BeforeAll
    public static void setUpBeforeClass() throws Exception {
        //打开博客登录页面
        webDriver.get("http://127.0.0.1:9580/sign-in.html");
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
    }

    /*
     * 检查登录页是否正确
     */
    @Order(1)
    @Test
    void LoginPageLoadRight() {
        //获取当前页面url
        String cur_url = webDriver.getCurrentUrl();
        Assertions.assertEquals("http://127.0.0.1:9580/sign-in.html", cur_url);
    }

    /*
     * 输入正确的账号密码，登陆成功
     * */
    @Order(3)
    @ParameterizedTest
    @CsvFileSource(resources = "LoginSuccess.csv")
    void LoginSuccess(String username, String nickname, String password) throws InterruptedException {
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);

        // 在每次登录之后都要进行清空，然后才能重新输入
        webDriver.findElement(By.cssSelector("#username")).clear();
        webDriver.findElement(By.cssSelector("#password")).clear();

        //输入长高admin
        webDriver.findElement(By.cssSelector("#username")).sendKeys(username);
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        //输入密码123
        webDriver.findElement(By.cssSelector("#password")).sendKeys(password);
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        //点击提交按钮
        webDriver.findElement(By.cssSelector("#submit")).click();


        //列表页用户信息是张三
        //昵称是张三就通过，否则测试不通过
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        String cur_nickname = webDriver.findElement(By.cssSelector("#index_nav_nickname")).getText();
        webDriver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);

        Assertions.assertEquals(nickname, cur_nickname);
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        sleep(3000);

        //返回登录页进行下一个测试
        //因为多参数，所以在执行完一遍执行下一遍的时候需要进行页面的回退，否则找不到登录框
        webDriver.navigate().back();


    }

    /*
     * 检查异常登录情况:
     * 多参数测试：为空 or 非空
     * 登录失败的检查元素和成功是不一样的
     */
    @Order(2)
    @ParameterizedTest
    @CsvSource({"小小周,123", "小蔡,xiaozhou"})
    void loginFail(String username, String password) {
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);

        // 在每次登录之后都要进行清空，然后才能重新输入
        webDriver.findElement(By.cssSelector("#username")).clear();
        webDriver.findElement(By.cssSelector("#password")).clear();

        webDriver.findElement(By.cssSelector("#username")).sendKeys(username);
        webDriver.findElement(By.cssSelector("#password")).sendKeys(password);
        webDriver.findElement(By.cssSelector("#submit")).click();


        // 登录
        // 用户名或密码为空
        try{
            // 等待invalid-feedback元素出现
            WebDriverWait wait = new WebDriverWait(webDriver, 10);
            wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("#invalid-feedback")));
            System.out.println("invalid-feedback 元素存在");
        } catch (Exception e) {
            System.out.println("invalid-feedback 元素不存在");
        }

        try {
            // 用户名或密码错误
            // 等待jq-toast-single jq-has-icon jq-icon-warning元素出现
            WebDriverWait wait = new WebDriverWait(webDriver, 10);
            wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("body > div.jq-toast-wrap.bottom-right > div")));
            System.out.println("用户名或密码错误");
        }catch (Exception e) {
            e.printStackTrace();
        }


    }


}
